//
//  NetInterceptor.swift
//  响应拦截器
//
//  Created by chen on 2025/5/1.
//
import SwiftUI

// 请求拦截器协议
public protocol NetInterceptor {
    
    // 请求发送前的处理
    func beforeRequest(_ request: URLRequest,_ queryParam:[String: Any]?,_ bodyParam: Any?) -> URLRequest
    
    // 响应接收后的处理
    func afterResponse(_ response: URLResponse,_ data: Data?) -> Void
}

// 提供默认实现
public class DefaultNetInterceptor: NetInterceptor {
        
    public init() {}
    
    public func beforeRequest(_ request: URLRequest,_ queryParam:[String: Any]?,_ bodyParam: Any?) -> URLRequest {
        //var mutableRequest = request
        
//        Logger.shared.info("url: \(mutableRequest.url?.absoluteString ?? "")")
//        
//        mutableRequest.addValue("application/json", forHTTPHeaderField: "Content-Type")
//        
//        if let queryParam = queryParam {
//            Logger.shared.info("请求头参数:\(queryParam)")
//        }
//        
//        if let bodyParam = bodyParam {
//            Logger.shared.info("请求体参数:\(bodyParam)")
//        }
        return request
    }
    
    public func afterResponse(_ response: URLResponse,_ data: Data?) -> Void {
//        if let httpResponse = response as? HTTPURLResponse {
//            Logger.shared.info("收到响应: 状态码 \(httpResponse.statusCode)")
//        }
//        
//        if let data = data {
//            Logger.shared.info("响应数据大小: \(data.count) bytes")
//            Logger.shared.info("响应数据：\(String(data: data, encoding: .utf8) ?? "")")
//        }
    }
}
